<?php
/**
 * Introduction: 商品属性逻辑处理
 * @author: 杨陈鹏
 * @date: 2016/4/10 20:00
 * @email: yangchenpeng@cdlinglu.com
 */

namespace Common\Logic;

use Common\Model;

class AttrLogic
{

    static $model_attr, $model_attr_val;

    public function __construct()
    {

        self::$model_attr = new Model\GoodsAttrModel();
        self::$model_attr_val = new Model\GoodsAttrValModel();

    }

    /**
     * Introduction 获取属性和属性值
     * @author 杨陈鹏
     * @date 2016/4/14 17:01
     * @email yangchenpeng@cdlinglu.com
     */
    static public function getData($where)
    {

        if (!$where)
            return ['code' => 300, 'msg' => '参数错误'];

        //获取属性

        $attrs = self::$model_attr
            ->where($where)
            ->select();

        if (!$attrs)
            return ['code' => 300, 'msg' => '暂无数据'];

        foreach ($attrs as $v) {

            $attr[$v['id']] = $v;

            $attr_vals .= $v['id'] . ',';

        }

        $attr_val = trim($attr_vals, ',');

        $attr_val = self::$model_attr_val
            ->where("attr_id IN($attr_val)")
            ->select();

        foreach ($attr_val as $v) {

            $attr_values[$v['attr_id']] = $v;

        }

        foreach ($attr as $k => $v) {

            $data[$k] = $v;

            foreach ($attr_val as $val) {

                if ($k == $val['attr_id'])
                    $data[$k]['attr_values'][] = $val;

            }

        }

        return ['code' => 200, 'data' => $data];

    }

    /**
     * Introduction 获取属性
     * @author 杨陈鹏
     * @date 2016/4/10 20:00
     * @email yangchenpeng@cdlinglu.com
     */
    static public function getAttrData()
    {

        $data = self::$model_attr->select();

        $category_logic = D('Common/Category','Logic');

        foreach($data as $k => $v) {

            $data[$k]['category_name'] = $category_logic::$model
                                ->where(['id' => $v['category_id']])
                                ->getField('name');

        }

        return $data;

    }

    /**
     * Introduction 添加属性
     * @author 杨陈鹏
     * @date 2016/4/10 20:00
     * @email yangchenpeng@cdlinglu.com
     * @param $data array 创建属性需要的数据   其中必须包含attr  attr_val字段且不能为空
     */
    static public function create($data)
    {

        if (!$data['attr'] || empty($data['attr_val']))
            return ['code' => 300, 'msg' => '参数错误'];

        $attr = $data['attr'];

        $attr_val = $data['attr_val'];

        if ($id = self::$model_attr->add(['name' => $attr, 'category_id' => $data['category_id']])) {

            foreach ($attr_val as $v) {

                self::$model_attr_val->add(['name' => $v, 'attr_id' => $id]);

            }

            return ['code' => 200, 'msg' => '添加成功'];

        } else {

            return ['code' => 300, 'msg' => '添加失败'];

        }

    }

    /**
     * Introduction 获取一条属性的数据
     * @author 杨陈鹏
     * @date 2016/4/10 20:00
     * @email yangchenpeng@cdlinglu.com
     * @param $attr_id int 属性ID
     */
    static public function getOneAttr($attr_id)
    {

        if (!$attr_id)
            return ['code' => 300, 'msg' => '参数错误'];

        //获取属性数据
        $data['attr'] = self::$model_attr->where(['id' => $attr_id])->find();

        if (!$data['attr'])
            return ['code' => 300, 'msg' => '缺少属性信息!'];
        //获取属性值数据

        $data['attr_val'] = self::$model_attr_val->where(['attr_id' => $attr_id])->select();

        return ['code' => 200, 'msg' => '获取数据成功', 'data' => $data];

    }

    /**
     * Introduction 获取属性值
     * @author 杨陈鹏
     * @date 2016/4/10 20:00
     * @email yangchenpeng@cdlinglu.com
     * @param $attr_id int 属性ID
     * @param $attr_val_id string or array 属性ID 格式
     */
    static public function getAttrVal($attr_id, $attr_val_id)
    {

        if (!$attr_id)
            return ['code' => 300, 'msg' => '参数错误'];

        if ($attr_val_id && is_array($attr_val_id)) {

            $attr_val_id = explode(',', $attr_val_id);

        }

        $where = "attr_id = $attr_id";

        if ($attr_val_id)
            $where .= " id IN($attr_val_id)";

        $data = self::$model_attr_val->where($where)->select();

        if ($data)
            return ['code' => 200, 'msg' => '数据获取成功', 'data' => $data];

        return ['code' => 300, 'msg' => '数据获取失败'];

    }

    /**
     * Introduction 修改属性
     * @author 杨陈鹏
     * @date 2016/4/10 22:53
     * @email yangchenpeng@cdlinglu.com
     * @param $data array 需要修改数据 必须包含id name字段
     */
    public function updateAttr($data)
    {

        if (!$data['id'] || !$data['name'])
            return ['code' => 300, 'msg' => '参数错误'];

        if (self::$model_attr->where(['id' => $data['id']])->save(['name' => $data['name']]))
            return ['code' => 200, 'msg' => '修改成功'];

        return ['code' => 300, 'msg' => '修改失败'];

    }

    /**
     * Introduction 修改属性值
     * @author 杨陈鹏
     * @date 2016/4/10 22:53
     * @email yangchenpeng@cdlinglu.com
     * @param $data array 需要修改数据 必须包含id name字段
     */
    public function updateAttrVal($data)
    {

        if (!$data['attr_id'] || !$data['id'] || !$data['name'])
            return ['code' => 300, 'msg' => '参数错误'];

        if (self::$model_attr_val->where(['attr_id' => $data['attr_id'], 'id' => $data['id']])->save(['name' => $data['name']]))
            return ['code' => 200, 'msg' => '修改成功'];

        return ['code' => 300, 'msg' => '修改失败'];

    }

    /**
     * Introduction 删除属性
     * @author 杨陈鹏
     * @date 2016/4/10 22:53
     * @email yangchenpeng@cdlinglu.com
     * @param $id int 属性ID
     */
    public function deleteAttr($id)
    {

        if (!$id)
            return ['code' => 300, 'msg' => '参数错误!'];

        if (self::$model_attr_val->where(['attr_id' => $id])->count())
            return ['code' => 300, 'msg' => '请先删除该属性的属性值!'];

        if (self::$model_attr->where(['id' => $id])->delete())
            return ['code' => 200, 'msg' => '删除成功'];

        return ['code' => 300, 'msg' => '删除失败!'];

    }

    /**
     * Introduction 删除属性值
     * @author 杨陈鹏
     * @date 2016/4/10 22:53
     * @email yangchenpeng@cdlinglu.com
     * @param $data array 删除条件  必须包含 attr_id id 两个字段
     */
    public function deleteAttrVal($data)
    {

        if (!$data['attr_id'] || !$data['id'])
            return ['code' => 300, 'msg' => '参数错误!'];

        if (self::$model_attr_val->where($data)->delete())
            return ['code' => 200, 'msg' => '删除成功'];

        return ['code' => 300, 'msg' => '删除失败!'];

    }

    /**
     * @introduction: 添加一个属性值
     * @author: 杨陈鹏
     * @date: 2015-04-10 16:22
     * @email: yangchenpeng@cdlinglu.com
     * @param $data array 属性值数据
     */
    public function createOneAttrVal($data)
    {
        if (!$data['attr_id'] || empty($data['name']))
            return ['code' => 300, 'msg' => '参数错误!'];


        foreach ($data['name'] as $v) {

            $i['id'] = self::$model_attr_val->add(['attr_id' => $data['attr_id'], 'name' => $v]);
            $i['name'] = $v;
            $return_data[] = $i;

        }

        return ['code' => 200, 'msg' => '添加成功!', 'data' => $return_data];

    }


}